package homework.threadwork2.coding;

import java.util.Random;

import static java.lang.Thread.sleep;

public class GenerateWeater implements Runnable {
    Weather w;
    Random rand = new Random();

    public GenerateWeater(Weather w) {
        this.w = w;
    }
    @Override
    public void run() {
        synchronized (this.w)
        {
            for(int i=0;i<10;i++){
//            if (!this.w.getFlag()) {
////                System.out.println("正在读取天气"+this.w.getFlag());
//                try {
//                    this.w.wait();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
            int randint1 = this.rand.nextInt(100);
            int randint2 = this.rand.nextInt(100);
            this.w.setHumidity(randint1);
            this.w.setTemp(randint2);
//            System.out.println("开始生成天气");
            System.out.printf("生成天气  温度%d湿度%d\n", this.w.getTemp(), this.w.getHumidity());

//            System.out.printf("生成天气  温度%d湿度%d\n", randint2, randint1);

            try {
                this.w.setFlag(false);
//                this.w.notifyAll();
                sleep(1000);

//                notifyAll();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


    }}
}
